# Create your views here.
from rest_framework import filters
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from utils.errormsg import erromsg
from utils.pagination import MyPagePagination
from utils.status import CODE_1, CODE_0
from .serializer import *
from .models import Urls


class UrlViews(ModelViewSet):
    queryset = Urls.objects.all()
    serializer_class = UrlSerializers
    permission_classes = [IsAuthenticated]
    pagination_class = MyPagePagination

    def newurls(self, request):
        user = request.user
        data = request.data
        moduleid = data.get("module_id")
        serializer = self.serializer_class(data=data)

        if not serializer.is_valid():
            return Response({**CODE_1, "msg": serializer.errors})
        module = Module.objects.filter(id=moduleid, project__user=user)
        if not module:
            return Response({**CODE_1, "msg": "模块不存在"})

        serializer.save()

        return Response({**CODE_0, "data": serializer.data})

    def urlslist(self, request):
        user = request.user
        queryset = self.queryset.filter(module__project__user_id=user.id)
        param = request.query_params
        method = param.get("method")
        url_name = param.get("url_name")
        urls = param.get("urls")
        projetc_name = param.get("projetc_name")

        if url_name:
            queryset = queryset.filter(url_name__contains=url_name)
        if method:
            queryset = queryset.filter(method=method)
        if urls:
            queryset = queryset.filter(urls__contains=urls)
        if projetc_name:
            queryset = queryset.filter(
                project__projectname__contains=projetc_name)
        queryset = self.paginate_queryset(queryset)
        serializer = self.serializer_class(queryset, many=True)
        data_dict = self.get_paginated_response(serializer.data)

        return Response({**CODE_0, **data_dict})

    def editurls(self, request, pk):
        user = request.user
        data = request.data
        queryset = self.queryset.filter(module__project__user_id=user.id,
                                        id=pk)
        if not queryset:
            return Response({**CODE_1, "msg": "不存在"})
        instance = queryset.get(id=pk)
        serializer = self.serializer_class(instance, data=data)
        if not serializer.is_valid():
            return Response({**CODE_1, "msg": erromsg(serializer)})
        serializer.save()

        return Response({**CODE_0, "data": serializer.data})

    def delurls(self, request, pk):
        user = request.user
        queryset = self.queryset.filter(module__project__user_id=user.id,
                                        id=pk)
        if not queryset:
            return Response({**CODE_1, "msg": "不存在"})
        queryset.delete()

        return Response({**CODE_0})
